package com.system.blogAdmin.auth.controller;

import com.cnc.common.web.util.MessageBox;
import com.cnc.common.web.util.SpringValidUtil;
import com.system.blogAdmin.auth.dto.AuthUserDto;
import com.system.blogAdmin.auth.dto.TreeDto;
import com.system.blogAdmin.auth.entity.AuthResource;
import com.system.blogAdmin.auth.entity.AuthRole;
import com.system.blogAdmin.auth.entity.User;
import com.system.blogAdmin.auth.service.AuthResourceService;
import com.system.blogAdmin.auth.service.AuthRoleService;
import com.system.blogAdmin.utils.DtoUtils;
import com.system.blogAdmin.utils.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者： cnc
 * 创建时间：2017-06-16.
 * 版本：1.0
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {

        @Autowired
        AuthResourceService authResourceService;
        @Autowired
        AuthRoleService authRoleService;


        @RequestMapping(value = "/index", method = RequestMethod.GET)
        public String index(){
            return "blogAdmin/auth/role_manage";
        }

        /**
         * 获取所有角色
         * @return
         */
        @RequestMapping(value = "/list",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public List<AuthRole> authRoleList(){
            return authRoleService.listAuthRole();
        }

        /**
         * 添加角色
         * @param authRole
         * @return
         */
        @RequestMapping(value = "/add", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public MessageBox addRole(@Valid AuthRole authRole,BindingResult result){
            if(result.hasErrors()){
                String errormsg = SpringValidUtil.getErrors2Str(result);
                return MessageBox.getErrorMsg(Properties.insertFailed + errormsg);
            }
            List<AuthRole>  authRoleList = authRoleService.listAuthRoleByName(authRole.getName());
            if(authRoleList.size() > 0){
                return MessageBox.getErrorMsg("已有相同角色名!");
            }
            authRoleService.create(authRole); //创建角色成功
            return MessageBox.getSuccessMsg(Properties.insertSuccess);
        }

    /**
     * 删除角色
     * @param id
     * @return
     */
        @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public MessageBox deleteRole(@PathVariable Long id){
            authRoleService.deleteById(id);
            return MessageBox.getSuccessMsg(Properties.deleteSuccess);
        }

    /**
     * 更新角色
     * @param authRole
     * @return
     */
        @RequestMapping(value = "/update", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public MessageBox updateRole(AuthRole authRole){
            authRoleService.update(authRole);
            return MessageBox.getSuccessMsg(Properties.updateSuccess);
        }

        /**
         * 根据id获取角色所拥有用户
         * @param id
         * @return
         */
        @RequestMapping(value = "/list/user/{id}", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public List<AuthUserDto> authUserDtoList(@PathVariable Long id){
            List<AuthUserDto> authUserDtoList = DtoUtils.toChange(authRoleService.listAuthUserByRoleId(id));
            return  authUserDtoList;
        }

        /**
         * 根据id获取角色所拥有的资源
         * @param id
         * @return
         */
        @RequestMapping(value = "/list/res/{id}", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public List<TreeDto> authResourcesList(@PathVariable Long id){
            return DtoUtils.res2treeDto(authRoleService.listAuthResourceByRoleId(id));
        }

        /**
         * 获取权限
         * @param roleId
         * @return
         */
        @RequestMapping(value ="/set/permission/list/{roleId}",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
        @ResponseBody
        public List<TreeDto>  getRoleListPermission(@PathVariable  Long roleId){
            List<AuthResource> allList = authResourceService.listResourseAll();
            List<AuthResource> roleList = authRoleService.listAuthResourceByRoleId(roleId);
            return DtoUtils.res2treeDto(allList, roleList);
        }

        /**
         * 保存修改后的权限
         * @param roleList
         * @param roleId
         * @return
         */
        @RequestMapping(value = "set/permission",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
        @ResponseBody
        public MessageBox setRolePermission(@RequestParam(value="roleList[]",required = false)List<Integer> roleList, Integer roleId){
            authRoleService.setPermission(roleList, roleId);
            return  MessageBox.getSuccessMsg(Properties.updateSuccess);
        }


}
